/* eslint-disable prettier/prettier */
// 监听事件
console.log('子线程创建成功');
console.log('---------');

self.onmessageerror = function(e){
	console.log("onmessageerror ",e);
}

self.onerror = function(e){
	console.log("onerror ",e);
}

onmessage = function(e){
	//接收参数
	let data = e.data;
	let entries_obj = data.entries_obj;
	
	data.result_data = {};
	
	console.log("接收数据",e);
	// console.log(voice_path);
	console.log(entries_obj);
	
	//if(entries_obj){
		let req = new XMLHttpRequest();
		let api_url = data.base_url + 'api/Appdownload/uploadVoiceByBase64';
		console.log("api_url",api_url);

		req.open("POST", api_url, false);
		req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		req.onreadystatechange = function() {
		  if (req.readyState === 4 && req.status === 200) {
		    // 请求成功
		    let response = req.responseText;
			console.log("response");
		    console.log(response);
			
			data.result_data = response;
		  }
		};
		req.send("suffix=" + data.suffix + "&basedata=" + encodeURIComponent(entries_obj));
		req.onload  = function(res) {
			console.log("XMLHttpRequest res");
			console.log(res);
			if (req.status  >= 200 && req.status  < 400) {
				console.log(req.responseText);
				
				// postMessage(req);
				
				// data.result_data = res;
			} else {
				console.log("null");
				console.log(req.statusText);
				//postMessage({code:1,msg:"文件上传失败",content:"状态：" + req.status});
			}
		};
		
		req.onerror = function (e) {
			console.log("onerror");
		  console.error(req.statusText);
		};
	//}
	data.entries_obj = "";
	postMessage(data.result_data);

}
